﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.Model;


namespace University.Workshop.DataAccess
{
    public class DataAccessWaterReadingControl
    {
        //public static ICollection<Model.WaterReadingControl> SearchWaterReadingByAllParameter(Model.KardexHelper kardexToFind)
        //{
        //    WFPEntities.Instance.DiscardChanges();

        //    var wrList = from wr in WFPEntities.Instance.WaterReadingControl
        //                 join k in WFPEntities.Instance.Kardex on wr.KardexId equals k.KardexId
        //                 where k.HasAWaterMeasurer == true &&
        //                       k.Enable == true &&
        //                       wr.IsValidReading == true &&
        //                      (wr.ReadingState == 0 ||
        //                       wr.ReadingState == 1) &&
        //                       k.Person.Names.StartsWith(kardexToFind.Name) &&
        //                       k.Person.LastNames.StartsWith(kardexToFind.LastName) &&
        //                       k.Allotment.FirstOrDefault().AllotmentName.StartsWith(kardexToFind.Allotment) &&
        //                       k.Allotment.FirstOrDefault().SquareId.Contains(kardexToFind.Square)
        //                 select wr;

        //    return wrList.ToList();
        //}

        public static ICollection<Model.WaterReadingControl> SearchWaterReadingByParameter(Model.KardexHelper kardexToFind)
        {
            WFPEntities.Instance.DiscardChanges();
            if (kardexToFind.Square != string.Empty && kardexToFind.Allotment != string.Empty)
            {
                var wrList = from wr in WFPEntities.Instance.WaterReadingControl
                             where wr.IsValidReading == true &&
                                   (wr.ReadingState == (short)Model.ReadingState.Nuevo || wr.ReadingState == (short)Model.ReadingState.Modificado) &&
                                   wr.Kardex.Person.Names.StartsWith(kardexToFind.Name) &&
                                   wr.Kardex.Person.LastNames.StartsWith(kardexToFind.LastName) &&
                                   wr.Kardex.Allotment.FirstOrDefault().AllotmentName.Equals(kardexToFind.Allotment) &&
                                   wr.Kardex.Allotment.FirstOrDefault().SquareId.Equals(kardexToFind.Square)
                             select wr;

                return wrList.ToList();
            }
            else
            {
                var wrList = from wr in WFPEntities.Instance.WaterReadingControl
                             where wr.IsValidReading == true &&
                                   (wr.ReadingState == (short)Model.ReadingState.Nuevo || wr.ReadingState == (short)Model.ReadingState.Modificado) &&
                                   wr.Kardex.Person.Names.StartsWith(kardexToFind.Name) &&
                                   wr.Kardex.Person.LastNames.StartsWith(kardexToFind.LastName) &&
                                   wr.Kardex.Allotment.FirstOrDefault().AllotmentName.StartsWith(kardexToFind.Allotment) &&
                                   wr.Kardex.Allotment.FirstOrDefault().SquareId.StartsWith(kardexToFind.Square)
                             select wr;

                return wrList.ToList();
            }
        }

        public static void RegisterNewWaterReadingControl(Model.WaterReadingControl newWaterReadingControl)
        {
            WFPEntities.Instance.DiscardChanges();
            WFPEntities.Instance.WaterReadingControl.Add(newWaterReadingControl);
            WFPEntities.Instance.SaveChanges();
        }

        public static void UpdataWaterReadingControl(Model.WaterReadingControl waterReadingControl)
        {
            WFPEntities.Instance.DiscardChanges();
            var q = (from p in WFPEntities.Instance.WaterReadingControl
                     where p.WaterReadingId.Equals(waterReadingControl.WaterReadingId)
                     select p).First();
            q.CurrentMeasurerRead = waterReadingControl.CurrentMeasurerRead;
            q.IsValidReading = waterReadingControl.IsValidReading;
            q.KardexId = waterReadingControl.KardexId;
            q.PaidAmount = waterReadingControl.PaidAmount;
            q.PreviousMeasurerRead = waterReadingControl.PreviousMeasurerRead;
            q.ReaderId = waterReadingControl.ReaderId;
            q.ReadingDate = waterReadingControl.ReadingDate;
            q.ReadingState = waterReadingControl.ReadingState;
            q.ReadingType = waterReadingControl.ReadingType;
            q.WaterCollectControlId = waterReadingControl.WaterCollectControlId;
            q.WaterFare = waterReadingControl.WaterFare;

            WFPEntities.Instance.SaveChanges();
        }

        public static ICollection<WaterReadingControl> GetByCurrentDate()
        {
            WFPEntities.Instance.DiscardChanges();
            return  (from p in WFPEntities.Instance.WaterReadingControl
                     where p.ReadingDate.Month == DateTime.Now.Month && p.ReadingDate.Year == DateTime.Now.Year
                     select p).ToList();
        }

        public static bool ExistAnyWaterReadingWithThisKardexIDAndDate(int kardexID, DateTime date)
        {
            var readings = (from wrc in WFPEntities.Instance.WaterReadingControl
                           where wrc.KardexId == kardexID && wrc.ReadingDate.Month == date.Month && wrc.ReadingDate.Year == date.Year && wrc.ReadingType == 0
                           select wrc).ToList ();

            return (readings.Count > 0 ? true : false);
        }


        public static ICollection<WaterReadingControl> SearchWaterReadingsMade()
        {
            WFPEntities.Instance.DiscardChanges();
            return (from p in WFPEntities.Instance.WaterReadingControl
                    where p.IsValidReading == true &&
                            p.ReadingState == (short)Model.ReadingState.Modificado
                    select p).ToList();
        }

        public static ICollection<WaterReadingControl> GetWaterReadingActiveByKardex(Kardex kardex)
        {
            WFPEntities.Instance.DiscardChanges();
            return  (from p in WFPEntities.Instance.WaterReadingControl
                        where p.IsValidReading == true &&
                                p.Kardex.KardexId == kardex.KardexId
                        select p).ToList();
        }
    }
}
